# FindRivs.cmake
# Usage: find_package(Rivs REQUIRED)
# Provides:
#   Rivs_FOUND
#   Rivs_INCLUDE_DIRS
#   Imported target: Rivs::Rivs (INTERFACE)

# Default RIVS_ROOT (if not provided)
if(NOT RIVS_ROOT)
  if(DEFINED ENV{RIVS_ROOT} AND NOT "$ENV{RIVS_ROOT}" STREQUAL "")
    set(RIVS_ROOT "$ENV{RIVS_ROOT}" CACHE PATH "Root prefix of Rivs installation" FORCE)
  else()
    set(RIVS_ROOT "/opt/rivs" CACHE PATH "Root prefix of Rivs installation" FORCE)
  endif()
endif()
message(STATUS "[FindRivs] RIVS_ROOT=${RIVS_ROOT}")

set(_RIVS_HINT_PREFIXES
    ${RIVS_ROOT}
    $ENV{RIVS_ROOT}
    $ENV{HOME}/yuan/enrigin_sdk
)

# Search include dir containing rivs/rivs_ext.h
set(_RIVS_INCLUDE_HINTS)
foreach(pfx IN LISTS _RIVS_HINT_PREFIXES)
  if(pfx)
    list(APPEND _RIVS_INCLUDE_HINTS
      ${pfx}/include
      ${pfx}/usr/include
      ${pfx}/opt/rivs/include
    )
  endif()
endforeach()

find_path(Rivs_INCLUDE_DIR
  NAMES rivs/rivs_ext.h
  HINTS ${_RIVS_INCLUDE_HINTS}
  PATHS /usr/include /usr/local/include /opt/rivs/include
)

include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(Rivs
  REQUIRED_VARS Rivs_INCLUDE_DIR
)

if(Rivs_FOUND)
  set(Rivs_INCLUDE_DIRS ${Rivs_INCLUDE_DIR})
  message(STATUS "[FindRivs] Include dir: ${Rivs_INCLUDE_DIR}")
  if(NOT TARGET Rivs::Rivs)
    add_library(Rivs::Rivs INTERFACE IMPORTED)
    set_target_properties(Rivs::Rivs PROPERTIES
      INTERFACE_INCLUDE_DIRECTORIES "${Rivs_INCLUDE_DIRS}"
    )
  endif()
endif()
